package Controller;
import java.util.List;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.naming.directory.SearchControls;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.ResponseWrapper;

import me.gacl.domain.Team;

import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.portlet.ModelAndView;

import Service.ServiceImp;
import Service.TeamService;

import com.alibaba.fastjson.JSON;

@Controller
public class TeamController {
//public class TeamController extends HttpServlet {
	// public ServiceImp = get();
	// private ServiceImp ServiceImp = new ServiceImp();
	@Resource(name = "teamService")
	private TeamService TeamService;

	public TeamService getTeamService() {
		return TeamService;
	}

	public void setTeamService(TeamService teamService) {
		TeamService = teamService;
	}

	// 锟斤拷锟斤拷锟斤拷
	/*public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=gbk");
		String flag = request.getParameter("flag");

		if (flag.equals("show")) {

			showTeam(request, response);
		} else if (flag.equals("add")) {
			addTeam(request, response);
		} else if (flag.equals("destroy")) {
			destroyTeam(request, response);
		} else if (flag.equals("edit")) {
			editTeam(request, response);
		} else if (flag.equals("search")) {
			searchTeam(request, response);
		}else if (flag.equals("sign")) {
			signTeam(request, response);
		}
	}

	// 通锟斤拷spring锟斤拷锟�

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);

	}*/

	/*public ServiceImp get() {

		// 锟斤拷锟斤拷锟斤拷锟斤拷锟铰�
		// 锟饺伙拷取servlet 锟斤拷锟斤拷锟斤拷
		ServletContext servletcontext = getServletContext();
		// 然锟斤拷 通锟斤拷servlet 锟斤拷锟斤拷锟侥伙拷取 spring 锟斤拷锟斤拷锟斤拷锟斤拷
		ApplicationContext context = WebApplicationContextUtils
				.getWebApplicationContext(servletcontext);
		// 通锟斤拷spring锟斤拷锟斤拷锟斤拷锟斤拷 锟斤拷取bean
		ServiceImp teamservice = (ServiceImp) context.getBean("teamService");
		return teamservice;
	}*/

	// 展示锟斤拷锟�
	@RequestMapping("/show")
	@ResponseBody
	public String showTeam(String teamname,String teamnumber,String page,String rows,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		//ServiceImp ServiceImp = get();
		// 锟斤拷锟斤拷
		//String teamname = request.getParameter("searchname");
		//String teamnumber = request.getParameter("searchnumber");

		//String pagenow = request.getParameter("page");
		//String pagesize = request.getParameter("rows");
	
         /* 设置格式为text/json    */
		response.setContentType("text/html;charset=GBK");
        String showlist = null;
		try {

		     showlist = TeamService.showTeam(teamname, teamnumber,
					page, rows);
		     response.setCharacterEncoding("UTF-8");
			 response.setCharacterEncoding("text/html,charset=utf-8");
		     //response.setContentType("application/json;charset=utf-8");

			System.out.println(showlist);
			
		} catch (Exception e) {
			System.out.println("servlet锟斤拷showTeam锟斤拷锟届常锟斤拷");
			e.printStackTrace();
		}
		//response.setCharacterEncoding("UTF-8");
		//response.setCharacterEncoding("text/html,charset=utf-8");
		
		return showlist;
	}

	// 锟斤拷锟斤拷锟斤拷锟�
	@RequestMapping("/add")
	@ResponseBody
	public String addTeam(String firstname,String lastname,HttpServletRequest request,HttpServletResponse response)
			throws ServletException, IOException {
		//ServiceImp ServiceImp = get();
		// 锟斤拷锟斤拷
		//String teamname = request.getParameter("firstname");
		//String teamnumber = request.getParameter("lastname");
	
		System.out.println("乱码"+firstname);
       int addlist = 0;
		try {

			addlist = TeamService.addTeam(firstname, lastname);

			System.out.println(addlist);
			//response.getWriter().println(addlist);
		} catch (Exception e) {
			System.out.println("servlet锟斤拷addTeam锟斤拷锟届常锟斤拷");
			e.printStackTrace();
		}
		return addlist+"";
	}
    
	@RequestMapping("/destroy")
	@ResponseBody
	public String destroyTeam(String tn) throws ServletException, IOException {
		//ServiceImp ServiceImp = get();
		// 锟斤拷锟斤拷
		//String teamname = request.getParameter("tn");
        int destroylist = 0;
		try {

			destroylist = TeamService.destroyTeam(tn);

			System.out.println(destroylist);
			//response.getWriter().println(destroylist);
		} catch (Exception e) {
			System.out.println("servlet锟斤拷destroyTeam锟斤拷锟届常锟斤拷");
			e.printStackTrace();
		}
		return destroylist+"";
	}
    
	@RequestMapping("/edit")
	@ResponseBody
	public String editTeam(String tn,String firstname,String lastname) throws ServletException, IOException {
		//ServiceImp ServiceImp = get();
		// 锟斤拷锟斤拷
		//String teamnameold = request.getParameter("tn");
		//String teamnamenew = request.getParameter("firstname");
		//String teamnumber = request.getParameter("lastname");
        int editlist = 0;
		try {

			editlist = TeamService.editTeam(tn,firstname,lastname);

			System.out.println(editlist);
			//response.getWriter().println(editlist);
		} catch (Exception e) {
			System.out.println("servlet锟斤拷editTeam锟斤拷锟届常锟斤拷");
			e.printStackTrace();
		}
		return editlist+"";
	}
    
	@RequestMapping("/search")
	@ResponseBody
	public String searchTeam(String searchname,String searchnumber,String page,String rows) throws ServletException, IOException {
		//ServiceImp ServiceImp = get();
		// 锟斤拷锟斤拷
		//String teamname = request.getParameter("searchname");
		//String teamnumber = request.getParameter("searchnumber");

		//String pagenow = request.getParameter("page");
		//String pagesize = request.getParameter("rows");
        Object flaglist=null;
        System.out.println("searching searchname:" +searchname);
        System.out.println("searching searchnumber:" +searchnumber);
        System.out.println("searching page:" +page);
        System.out.println("searching rows:" +rows);
		try {

			 flaglist = TeamService.searchTeam(searchname, searchnumber,
					 page, rows);

			System.out.println("danel watson searching:::::"+flaglist);
			//response.getWriter().println(flaglist);
		} catch (Exception e) {
			System.out.println("servlet锟斤拷searchTeam锟斤拷锟届常锟斤拷");
			e.printStackTrace();
		}
		return flaglist+"";
	}
	
	// 锟斤拷证锟斤拷锟�
	@RequestMapping("/sign")
	@ResponseBody
	public String signTeam(String team,String number) throws ServletException, IOException {
		//ServiceImp ServiceImp = get();
		// 锟斤拷锟斤拷
		//team = request.getParameter("team");
	   // number= request.getParameter("number");
		System.out.println("controller"+team);
		System.out.println("controller"+number);
		int flag = 0;
		String flagstr = null;
		try {

		    flag = TeamService.signTeam(team, number);

			System.out.println(flag);
			//response.getWriter().println(flag);
		} catch (Exception e) {
			System.out.println("servlet锟斤拷signTeam锟斤拷锟届常锟斤拷");
			e.printStackTrace();
		}
		
		
		return flag+"";
		
	}
	
	@RequestMapping("rual")
	@ResponseBody
	public String testTeam() {
		
            return "Emiya is a cool girl";
			//System.out.println(flag);
			//response.getWriter().println(flag);
		//} catch (Exception e) {
		//	System.out.println("servlet锟斤拷signTeam锟斤拷锟届常锟斤拷");
		//	e.printStackTrace();
		//}
	}
	@RequestMapping("/Watson")//使锟斤拷EL
	public String watsonTeam(String team,String number, HttpServletRequest request) throws ServletException, IOException {
			
			 //String userJsonString = request.getParameter("userJsonString");
	        
			int flag = 0;
			String flagstr = null;
			System.out.println("Watson.do:::"+team);
			
			//request.setAttribute("team", team); 
			flag = TeamService.signTeam(team,number);
			if(flag==1){
				request.setAttribute("team", team); 
				request.setAttribute("username", team); 
			   return "teamlist";
			}else{
			   return "index";
			}

		
	}
	
	@RequestMapping("/snack")//锟斤拷锟斤拷锟斤拷锟絪nack.do页锟斤拷
    @ResponseBody  //ajax使锟斤拷 锟斤拷锟截碉拷锟斤拷值
    public String load1(@RequestParam String name,@RequestParam String password) throws IOException{
        System.out.println(name+" : "+password);  
        return name+" : "+password;
       
    }
	
	@RequestMapping("/signex")
	@ResponseBody
	public String signexTeam(String teamJsonString) throws ServletException, IOException {
		
		 //String userJsonString = request.getParameter("userJsonString");
         Team team = JSON.parseObject(teamJsonString, Team.class);
         System.out.println(team);
		int flag = 0;
		String flagstr = null;
		try {

		    flag = TeamService.signexTeam(team);

			System.out.println(flag);
			//response.getWriter().println(flag);
		} catch (Exception e) {
			System.out.println("servlet锟斤拷signTeam锟斤拷锟届常锟斤拷");
			e.printStackTrace();
		}
		
		
		return flag+"";
		
	}
	@RequestMapping(value="/Emiya")  //锟铰斤拷ModelMap model锟斤拷锟斤拷实锟斤拷Beam锟斤拷锟斤拷HashMap锟斤拷锟斤拷锟斤拷锟�
	public String login(String username, ModelMap model,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{  
	    //String-Freemarker
		request.setAttribute("username", username);
		String luanmaflag = TeamService.luanma(username);
		request.setAttribute("luanmaflag", luanmaflag);
		
		//Bean.class-Freemarker
		Team teams = new Team();
		teams.setId(2016);
		teams.setNumber(920);
		teams.setTeam("Danel-Watson");
		model.addAttribute("teams", teams);
		
        //List-FreeMarker
		Team team1 = new Team();
		team1.setId(2012);
		team1.setNumber(75);
		team1.setTeam("Leon-Ashely");
		Team team2 = new Team();
		team2.setId(2013);
		team2.setNumber(55);
		team2.setTeam("Emiya-Irish");
		Team team3 = new Team();
		team3.setId(2014);
		team3.setNumber(61);
		team3.setTeam("Ada-Chris");
		Team team4 = new Team();
		team4.setId(2015);
		team4.setNumber(64);
		team4.setTeam("Jill-Kim");
		List<Team> list = new ArrayList<Team>();
		list.add(team4);
		list.add(team3);
		list.add(team2);
	    list.add(team1);
		model.addAttribute("list", list);
		
        //HashMap-FreeMarker
		Map<String, String> testMap = new HashMap<String, String>();
		testMap.put("name", "Watson");
		testMap.put("rank", "7");
		model.addAttribute("testMap", testMap);
		 
		 
		
	     
		
	    
	    return "EL";
	}  


}
